package me.zingon.MapDemo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import me.zingon.MapDemo.dao.BusLineDao;
import me.zingon.MapDemo.model.BusLine;
import me.zingon.MapDemo.model.VehiclePos;
import me.zingon.MapDemo.service.BusLineService;
import me.zingon.MapDemo.service.NetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by ZTC on 3/23/2017.
 */
@Service
public class BusLineServiceImpl implements BusLineService{

    @Autowired
    NetService netService;

    @Value("${bus.queryBusLines}")
    String getBusLinesUrl;

    @Value("${bus.VehiclePos}")
    String vehiclePosUrl;

    @Autowired
    BusLineDao busLineDao;

    Map<String,BusLine> busLineMap;

    @PostConstruct
    @Transactional
    public void loadBusLineMap(){
        busLineMap=new HashMap<>();
        List<BusLine> list=busLineDao.getBusLineByName(null);
        for(BusLine l:list){
            busLineMap.put(l.getBusLineId(),l);
        }
    }

    @Override
    @Transactional
    public Object getBusLines(){
        List<BusLine> list=new ArrayList<>();
        Map<String,String> params=new HashMap<>(1);
        for(int i=0;i<=9;i++){
            params.put("BUS_LINE_NAME",String.valueOf(i));
            String result= netService.post(getBusLinesUrl,params);
            JSONObject jsonObject= JSON.parseObject(result);
            for(Object o:jsonObject.getJSONArray("busLines")){
                BusLine bl=new BusLine();
                bl.setBusLineId(((JSONObject)o).getString("busLineId"));
                bl.setBusLineName(((JSONObject)o).getString("busLineName"));
                bl.setUpdateDate(new Date());
                list.add(bl);
            }
        }
        busLineDao.saveLines(list);
        return list;
    }


    @Override
    @Transactional
    public List<VehiclePos> getNewPos(){
        return busLineDao.getNewPos();
    }

    @Override
    @Transactional
    public List<VehiclePos> getAllPos(){
        List<VehiclePos> result=new ArrayList<>();
        Set<Map.Entry<String,BusLine>> set=busLineMap.entrySet();
        for(Map.Entry<String,BusLine> e:set){
            result.addAll(getVehiclePos(e.getValue(),"1"));
            result.addAll(getVehiclePos(e.getValue(),"2"));
        }
        busLineDao.saveVehiclePos(result);
        return result;
    }


    @Override
    public List<VehiclePos> getVehiclePos(BusLine bl,String flag){
        List<VehiclePos> result=new ArrayList<>();
        Map<String,String> params=new HashMap<>(3);
        params.put("BUS_LINE_NAME",bl.getBusLineName());
        params.put("BUS_LINE_ID",bl.getBusLineId());
        params.put("FLAG",flag);
        JSONObject resp=JSON.parseObject(netService.post(vehiclePosUrl,params));
        JSONArray list=resp.getJSONArray("vehiclePos");
        for(Object o:list){
            VehiclePos vp=new VehiclePos();
            vp.setCarId(((JSONObject)o).getString("carId"));
            vp.setFlag(Integer.valueOf(((JSONObject)o).getString("flag")));
            vp.setLng(((JSONObject)o).getString("lng"));
            vp.setLat(((JSONObject)o).getString("lat"));
            vp.setPosition(Integer.valueOf(((JSONObject)o).getString("position")));
            vp.setStationName(((JSONObject)o).getString("stationName"));
            vp.setBusLineName(resp.getString("busLineName"));
            vp.setBusLineId(resp.getString("busLineId"));
            vp.setOld(false);
            vp.setUpdateDate(new Date());
            result.add(vp);
        }
        return result;
    }

}
